// miniprogram/pages/Players/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    baseimgaeUrl: app.globalData.imageBaseUrl,
    logoUrl:app.globalData.imagelogourl,
    _py: [ "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
    players:[],
    sponsorlogoes:[],
    currentTab:1,
    altheword:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getmemebers();
      this.setData({
        sponsorlogoes:app.globalData.sponsorlogoes,
        logoUrl:app.globalData.imagelogourl,
      });
      var that = this;
      if(!this.data.sponsorlogoes){
        app.getlogoes(function(res){
          if(res.data.data){
            console.log("sponsorlogoes");
            console.log(res.data.data);
            that.setData({sponsorlogoes:res.data.data});
            app.globalData.sponsorlogoes = that.data.sponsorlogoes;
          }
        
        },0);
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: 3
    })
    }
  },
  onResize:function(res) {
    this.onShow();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
       // 显示标题栏进度条效果
    wx.showNavigationBarLoading();


    this.setData({memberpageno:0,altheword:''});
    this.getmemebers();



   

    // 取消标题栏进度条效果
    wx.hideNavigationBarLoading();
    // 取消页面刷新动画
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     this.getmemebers();
  },

 

  getplayerbyaltheword(e){
    var altheword = e.currentTarget.dataset.searchaltheword;

    this.setData({
      altheword:altheword,
      memberpageno:0,
    })
  
    this.getmemebers(altheword);
  },

  clearaltheword(){
    this.setData({
      altheword:'',
      memberpageno:0,
    })
  
    this.getmemebers();
  },
  

  //获取首页的会员信息
  getmemebers:function(altheword){
    if(this.data.isloading){
      return;
    }
    var that = this;   //getmemebers: function (index, matchid, sex_id,pageno, type, fnSuccse, keywords, fnfinal) {
      that.setData({isloading:true,})
   app.getmemebers("", 0,"",that.data.memberpageno, "all", function(res){
        console.log("getmemebers-res");
        console.log(res);
        if(res.data.data && res.data.data.leaders){
          that.setData({leaders: res.data.data.leaders});
        }
        if(res.data.data && res.data.data.players){
          if(that.data.memberpageno == 0){
            that.setData({players:res.data.data.players,memberpageno:res.data.data.pageno,totalmembercount:res.data.data.totalcount});
            console.log("that.data.getmemebers");
            console.log(that.data.players);
            }
            else{
                var players = that.data.players;
                players = players.concat(res.data.data.players);
                that.setData({players:players,memberpageno:res.data.data.pageno,totalmembercount:res.data.data.totalcount});
            }
            console.log(that.data.players);
            that.setData({isbottom:false});
        }
        else{
          if(that.data.memberpageno>0){
            that.setData({isbottom:true});
          }
          else{
            console.log("that.data.players:[]");
            that.setData({players:[]});
          }
        }
   },this.data.altheword,function(){
          that.setData({isloading:false,})
   }) ;
 
},
 ///获取球员信息
 getplayers(altheword)
 {
   var that =this;
   wx.showLoading({
        title: '加载中',
      })
      app.getplayers ("", 0,that.data.currentTab-1, "all", function(res){
        if(res.data.data.players&& res.data.data.players.length>0){
          console.log("getplayers");
          console.log(res);
          that.setData({players:res.data.data.players});
        }
        else{
          that.setData({players:[]});
        }
      }, (altheword)?altheword:'',
      function(){
        wx.hideLoading()
      });
       
         
        
    
 },

  //点击用户名字或卡片 直接弹出球员介绍页面
  _openplayerinfo:function(e){

    
    let playerid = e.currentTarget.dataset.playerid;
    console.log(" _openplayerinfo");
    console.log(playerid);
    wx.navigateTo({
      url: '../Players/playerinfo/index?selectid='+playerid,
    })
    
 },
 ///切换菜单选项
 navTabChange(e) {
  var selected = e.currentTarget.dataset.currenttab;

  this.setData({
    currentTab: selected
  })

  this.getplayers(this.data.altheword);

},
})